package bimvu.lab1;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class SubActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);

        updateTransitionsCounter();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            Object foreigner = bundle.get("Item1");
            try {
                Person buddy = (Person) foreigner;
                ((TextView) this.findViewById(R.id.nameOfThePerson)).setText(buddy.toString());
            } catch (NullPointerException e) {
                Log.w(this.getClass().getName(), "upset... is just Null");

            } catch (ClassCastException e) {
                Log.w(this.getClass().getName(), "A monster comes instead of Person :" + foreigner.getClass().getName());

            }
        }
    }

    private void updateTransitionsCounter()
    {
        TransitionsCounter counter = TransitionsCounter.getInstance();
        counter.increment();
        View counterLayout = findViewById(R.id.include2);
        View counterView = counterLayout.findViewById(R.id.transitionsCount);
        ((TextView) counterView).setText(counter.getValue().toString());
    }

    public void handleGoBack(View target)
    {
        switch (target.getId()) {
            case R.id.goBack:
                setResult(RESULT_OK, null);
                finish();
                break;
            default:
                finish();
                break;
        }
    }

}
